import request from '@/router/axios';
import website from "@/config/website";

export const registerUser = (form) => request({
  url:'/api/blade-user/register',
  method:'post',
  data:form,
})


export const loginByUsername = ( username, password, type, key, code) => request({
  url: '/api/blade-auth/oauth/token',
  method: 'post',
  headers: {
    // 'Tenant-Id': tenantId,
    'Captcha-Key': key,
    'Captcha-Code': code,
  },
  params: {
    // tenantId,
    username,
    password,
    grant_type: (website.captchaMode ? "captcha" : "password"),
    scope: "all",
    type
  }
});

export const genTokenKey = (param) => {
  $.ajax({
    url: '/api/blade-auth/oauth/gen-token-key',
    headers: param.headers,
    async: !param.sync,
    type: 'GET',
    data: {info: param.info},
    success: function (data) {
      if(param.success) {
        param.success(data);
      }
    },
    error: function () {
      if(param.error) {
        param.error();
      }
    }
  });
};


export const loginByToken = ( tokenKey) => request({
  url: '/api/blade-auth/oauth/token',
  method: 'post',
  params: {
    tokenKey,
    grant_type: "token",
    scope: "all",
  }
});

export const loginBySocial = (tenantId, source, code, state) => request({
  url: '/api/blade-auth/oauth/token',
  method: 'post',
  headers: {
    'Tenant-Id': tenantId
  },
  params: {
    tenantId,
    source,
    code,
    state,
    grant_type: "social",
    scope: "all",
  }
})

export const refreshToken = (refresh_token, tenantId) => request({
  url: '/api/blade-auth/oauth/token',
  method: 'post',
  headers: {
    'Tenant-Id': tenantId
  },
  params: {
    tenantId,
    refresh_token,
    grant_type: "refresh_token",
    scope: "all",
  }
});

export const registerGuest = (form, oauthId) => request({
  url: '/api/blade-user/register-guest',
  method: 'post',
  params: {
    tenantId: form.tenantId,
    name: form.name,
    account: form.account,
    password: form.password,
    oauthId
  }
});

export const getButtons = () => request({
  url: '/api/blade-system/menu/buttons',
  method: 'get'
});

export const getCaptcha = () => request({
  url: '/api/blade-auth/oauth/captcha',
  method: 'get'
});

export const logout = () => request({
  url: '/api/blade-auth/oauth/logout',
  method: 'get'
});

export const getUserInfo = () => request({
  url: '/api/blade-auth/oauth/user-info',
  method: 'get'
});

export const sendLogs = (list) => request({
  url: '/api/blade-auth/oauth/logout',
  method: 'post',
  data: list
});

export const clearCache = () => request({
  url: '/api/blade-auth/oauth/clear-cache',
  method: 'get'
});

//激活密码
export const activationPassword = (password,userId) =>request({
  url:'/api/blade-user/setPassword',
  method:'post',
  params:{
    password,
    userId,
  }
})

/**
 * 获取租户底下所有用户
 */
export const getAllUser = (param = {}) =>request({
  url:'/api/blade-user/select/user',
  method:'get',
  params: {
    ...param,
  }
})

/**
 * 获取租户底下所有存在用户（包括空用户）
 */
export const getExistUser = () => request({
  url: '/api/blade-user/get-exist-user',
  method: 'get'
})


/**
 * 获取角色
 */
export const getRoles = ()=>request({
  url:"/api/blade-system/tendvariity/role-enum",
  method:'get'
})
